package com.wxlgroup.springboot.controller;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.wxlgroup.springboot.commons.Response;
import com.wxlgroup.springboot.entity.User;
import com.wxlgroup.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @auther:wxl
 * @Date:2022/5/20-05-20-17:14
 * @Description:com.wxlgroup.springboot.controller
 * @Version:1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/queryAll")
    public Response queryALLUsers(){
        return userService.queryAll();
    }

    @PostMapping("/insert")
    public Response insertUser(@RequestBody User user){
         return userService.save(user);
    }

    @GetMapping("/query")
    public Response queryUserById(Integer id){
        return userService.queryUserById(id);
    }

    @DeleteMapping("/delete/{id}")
    public Response deleteUserById(@PathVariable Integer id){
        return userService.deleteUserById(id);
    }

    @PostMapping("/deleteUsers")
    public Response deleteUsers(@RequestBody List<Integer> ids){
        return userService.deleteUsers(ids);
    }


    @PostMapping("/register")           //用户注册接口
    public Response register(@RequestBody User user){
        return userService.save(user);
    }


    @GetMapping("/page")
    public Response findUserCounts(
             Integer pageNumber,
             Integer pageSize,
             String username,
             String email,
             String address){
        return userService.findUsersByPage(pageNumber,pageSize,username,email,address);
    }




    @GetMapping("/export")          //将用户信息导出为excel文件接口
    public void  export(HttpServletResponse response) throws IOException {
        List<User> users = userService.exportUsers();
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(users, true);
        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
    }



    @PostMapping("/import")         //将批量用户信息导入数据库
    public void importUsers(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<User> users = reader.readAll(User.class);
        for (User user : users) {
            user.setId(null);
            userService.save(user);
        }
    }


    @GetMapping("/queryByUsername")
    public Response userInformation(String username){
        return userService.findUserByUsername(username);
    }

}
